package web

import (
	"fmt"
	"github.com/golang/glog"
	"io"
	"net/http"
	"os"
	"strconv"
)

/*
 *	根处理器
 */
func RootHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("this is the root")
	io.WriteString(w, "hello world")

}

/*
 *	1. 接收客户端 request，并将 request 中带的 header 写入 response header
 */
func WriteHeaderToResponse(w http.ResponseWriter, r *http.Request) {
	for k, v := range r.Header {
		fmt.Println("header key and value is", k, v)
		for _, v2 := range v {
			w.Header().Set(k, v2)
		}

	}
}

/**
  2. 读取当前系统的环境变量中的 VERSION 配置，并写入 response header
*/
func SetOSVersion(w http.ResponseWriter, r *http.Request) {

	envVersion := os.Getenv("VERSION")
	w.Header().Set("VERSION", envVersion)

}

/**
3. Server 端记录访问日志包括客户端 IP，HTTP 返回码，输出到 server 端的标准输出
*/
func GerIPAndStatusCode(w http.ResponseWriter, r *http.Request) {

	ipString := r.Host
	glog.V(2).Info("request host is ", ipString)
	io.WriteString(w, "request host is "+ipString)

}

/**
4. 当访问 localhost/healthz 时，应返回200
*/
func SetResponseStatusCode(w http.ResponseWriter, r *http.Request) {
	code := 200
	w.WriteHeader(code)
	io.WriteString(w, "statusCode is "+strconv.Itoa(code))

}
